<?php
/**
 * @name          wx-qq-function    
 * @copyright     http://code.google.com/u/wxframework/
 * @version       $Id: file.class.php 26 2009-04-15 01:19:51Z sean.fmais $
 * @author        $Author: sean.fmais $  <fmais@163.com>
 * 
**/

class cls_file{

	var $path;
	var $name;
	var $result;
/**
  * creat a file
  * @param string $path
  * @param string $name
  */ 
function creat_file($path,$name)
{  
  $filename=$path.$name;
  if (file_exists($filename)){
    return false; # File already exists, please change the file name
  }else{
   if (file_exists($path)){
    touch($name);
    rename($name,$filename);
    return true; #Document set up successful
   }else{
     return false; #Directory does not exist, please check
   }
  }
}

/**
  *
  * @param string_type $path
  * @param string_type $name
  * @param string_type $content
  * @param bool_type $bool   (1 write over file , 2 edit file)
  */
function write_file($path,$name,$content,$bool)
{
  $filename=$path.$name;
  
  if ($bool==1) {
   if (is_writable($filename)) {
		   $handle=fopen($filename,'a');
		   if (!$handle) {exit;}#Can not open file or file does not exist
		   $result=fwrite($handle,$content);
		   if (!result) {exit;}#File write failed
		   fclose($handle);
		   return true;
	}else{
		   return false;#File does not exist
	}
  }
  
  if ($bool==2) {
     if (!file_exists($filename)) {
		$this->creat_file($path,$name);
		$handle=fopen($filename,'a');
		if (fwrite($handle,$content));
		 return true;
	    }else {
			unlink($filename);
			$this->creat_file($path,$name);
			return $this->write_file($path,$name,$content,1);
	   }
  }
  
}

/**
  *
  * @param string_type $path
  * @param string_type $name
  */
function del_file($path,$name){
  $filename=$path.$name;
  
  if (!file_exists($filename)) {
       return false; #File does not exist, make sure the path is correct
  }else{
	   if (unlink($filename)){
	     return true; #Successfully delete files
	   }else{
	     return false; #Delete file failed
	   }
  }
  
}

/**
  *
  * @param string_type $path
  * @param string_type $name
  */
function modi_file($path,$name){ 
  $filename=$path.$name;
  $handle=fopen($filename,'r+');
  $content=file_get_contents($filename);
  echo "<form id='form1' name='form1' action='modi_file.php' method='post'>";
  echo "<textarea name=content rows='10'>$content</textarea>";
  echo "<p>PathName：<input type='text' name='filename' value=$filename /><p>";
  echo "<input name=ss type=submit value=submit />";
  echo "</form>";
}

/**
  *
  * @param string $name
  * @param string $spath
  * @param string $dpath
  */
function copy_file($name,$spath,$dpath) 
{
  $filename=$spath.$name;
  if (file_exists($filename)) {
     $handle=fopen($filename,'a');
     copy($filename,$dpath.$name);
     if (file_exists($dpath.$name)){ return true;}
     return false;
  }else{
     return false;
  }
}

/**
  *
  * @param string_type $path
  * @param string_type $dirname
  * @param string_type $dpath
  */
function move_file($name,$spath,$dpath) 
{
  $filename=$spath.$name;
  if (file_exists($filename) && file_exists($dpath)) {
     $result=rename($filename,$dpath.$name);
     return $result;
  }else{
    return false; #File does not exist can not be moved
  }
}

/**
  *
  * @param string_type $filename
  * @param string_type $newname
  */
function rename_file($filename,$newname) { 
   $path=pathinfo($filename);
   $dir=$path['dirname']; 
   $newfilename=$dir.$newname;
   if (file_exists($filename)) { 
      $result=rename($filename,$newfilename);
	  return $result;
	}else{
	  return false;
	}
}

/**
  *
  * @param string_type $path
  * @param int_type $switch  (1 list and filename , 2 only list , 3  only filename)
  */
function list_filename($path,$switch=1) {
  if (file_exists($path)) {
   $dir=scandir($path);
   if ($switch==1){
    for ($i=0;$i<=count($dir);$i++)
    {
     if ($dir[$i]!="." and $dir[$i]!="..") 
     {
      echo "$dir[$i] <br />";
     }
    }
   }
   if ($switch==2) 
   {
    for ($i=0;$i<=count($dir);$i++)
    {
     $x=is_dir($path.$dir[$i]);
     if ($dir[$i]!="." and $dir[$i]!=".." and $x==true)
     {
      echo "$dir[$i] <br />";
     }
    }
   }
   if ($switch==3)
   {
    for ($i=0;$i<=count($dir);$i++)
    {
     $x=is_dir($path.$dir[$i]);
     if ($dir[$i]!="." and $dir[$i]!=".." and $x==false)
     {
      echo "$dir[$i] <br />";
     }
    }
   } 
  }
}

/**
  *
  * @param string_type $path
  * @param string_type $dirname
  */
function creat_dir($path,$dirname){ 
	  if (file_exists($path)) {
	   $result=mkdir($path.$dirname);
	   return $result;
	  }
	  
	   return false;
}

/**
  *
  * @param string_type $pathname
  */
function del_dir($pathname){
		  if (!is_dir($pathname)){exit('Please check the directory is correct');}
		
		  $handle=opendir($pathname);
		  while (($fileordir=readdir($handle)) !== false) {
		   if ($fileordir!="." && $fileordir!="..") {
			 is_dir("$pathname/$fileordir")?$this->del_dir("$pathname/$fileordir"):unlink("$pathname/$fileordir");
		   }
		  }
		  
		  if (readdir($handle) == false){
				 closedir($handle);
				 rmdir($pathname);
		  }
}

}

?>